const request = require('supertest')
const app = require('../app')
const should = require('chai').should()
const expect = require('chai').expect

let myToken = ''
const user = {
  username: 'root',
  password: '200050'
}

const user1 = {
  username: 'sss',
  password: '12345'
}

const user2 = {
  username: 'YcsaK1ko',
  password: 'ljh200050'
}
const server = app.listen(9900)

before(async function () {
  const res = await request(server)
    .post('/learningSpace/login')
    .set('Content-Type', 'application/json')
    .send(JSON.stringify(user))

  const { data: { token } } = res.body
  myToken = token
})

describe('测试管理员模块', function () {
  it('测试获取用户列表，信息正常', async function () {
    const res = await request(server)
      .get('/learningSpace/admin/userList')
      .set('Authorization', 'Bearer ' + myToken)
      .query('limit=10')
    expect(res.body.status).to.equal(200)
    expect(res.body.data).have.property('total')
    expect(res.body.data).have.property('list').with.lengthOf(10)
  })

  it('错误权限测试', async function () {
    const res = await request(server)
      .post('/learningSpace/login')
      .set('Content-Type', 'application/json')
      .send(JSON.stringify(user2))
    const { data: { token } } = res.body
    const response = await request(server)
      .get('/learningSpace/admin/userList')
      .set('Authorization', 'Bearer ' + token)
      .query('limit=10')
    expect(response.body.status).to.equal(401)
    expect(response.body.message).to.equal('此身份请求不是管理员')
  })

  it('缺失每页数量', async function () {
    const res = await request(server)
      .get('/learningSpace/admin/userList')
      .set('Authorization', 'Bearer ' + myToken)
      .set('Content-Type', 'application/json')
    expect(res.body.status).to.equal(403)
    expect(res.body.message).to.equal('缺失每页具体数量')
  })
  it('修改信息', async function () {
    const answer = {
      id: '609925d41e6fb01380ee452a',
      learnStyle: 1
    }
    const res = await request(server)
      .put('/learningSpace/admin/change')
      .set('Authorization', 'Bearer ' + myToken)
      .set('Content-Type', 'application/json')
      .send(JSON.stringify(answer))
    expect(res.body.status).to.equal(200)
    expect(res.body.message).to.equal('信息修改成功')
  })

  it('修改信息用户名重复', async function () {
    const answer = {
      id: '609925d41e6fb01380ee452a',
      username: '饮茶先啦'
    }
    const res = await request(server)
      .put('/learningSpace/admin/change')
      .set('Authorization', 'Bearer ' + myToken)
      .set('Content-Type', 'application/json')
      .send(JSON.stringify(answer))
    expect(res.body.status).to.equal(403)
    expect(res.body.message).to.equal('用户名已存在，请重新设置')
  })

  it('测试查询', async function () {
    const res = await request(server)
      .get('/learningSpace/admin/find')
      .set('Authorization', 'Bearer ' + myToken)
      .query('limit=10')
      .query('learnStyle=1')
    expect(res.body.status).to.equal(200)
    expect(res.body.data).have.property('total')
    expect(res.body.data).have.property('list')
  })

  it('修改信息只有id', async function () {
    const answer = {
      id: '609925d41e6fb01380ee452a',
    }
    const res = await request(server)
      .put('/learningSpace/admin/change')
      .set('Authorization', 'Bearer ' + myToken)
      .set('Content-Type', 'application/json')
      .send(JSON.stringify(answer))
    expect(res.body.status).to.equal(403)
    expect(res.body.message).to.equal('请检查请求参数，参数错误或缺少必要条件，请求值必须携带用户id且不能只有id')
  })

  it('修改信息缺少id', async function () {
    const answer = {
      username: '饮茶先啦'
    }
    const res = await request(server)
      .put('/learningSpace/admin/change')
      .set('Authorization', 'Bearer ' + myToken)
      .set('Content-Type', 'application/json')
      .send(JSON.stringify(answer))
    expect(res.body.status).to.equal(403)
    expect(res.body.message).to.equal('请检查请求参数，参数错误或缺少必要条件，请求值必须携带用户id且不能只有id')
  })

  it('id错误', async function () {
    const answer = {
      id: '409925d41e6fb01380ee452a',
      username: '饮茶先'
    }
    const res = await request(server)
      .put('/learningSpace/admin/change')
      .set('Authorization', 'Bearer ' + myToken)
      .set('Content-Type', 'application/json')
      .send(JSON.stringify(answer))
    expect(res.body.status).to.equal(403)
    expect(res.body.message).to.equal('蠢货!用户id错误')
  })
})
